from pathlib import Path

import pandas as pd

dir_path = r'D:\CodingSapce\labelmeWeb\flask-project\project_name\exist_excel'

file_path = r'D:\CodingSapce\labelmeWeb\flask-project\project_name\exist_excel\caorunkai_20230613_10184090.xlsx'
path = Path(file_path)
df = pd.read_excel(path, sheet_name='Sheet1')

jarr = []

for index, row in df.iterrows():
    groups = {
        '方向': [],
        '鼻腔粘膜': [],
        '鼻中隔': [],
        '总鼻道/鼻腔': [],
        '下鼻甲': [],
        '中鼻甲': [],
        '中鼻道': [],
        '嗅裂': [],
        '鼻咽': [],
    }
    json_data = {
        'imageName': '',
        'feature': groups,
    }
    if index != -1:
        for column_name in row.keys():
            if column_name == '图片名称':
                json_data['imageName'] = row[column_name]
            else:
                for group_name in groups.keys():
                    if group_name in column_name:
                        data = row[column_name] if not pd.isna(
                            row[column_name]) else 1
                        json_data['feature'][group_name].append(
                            {
                                column_name.replace(group_name, ''): int(data)
                            }
                        )
    jarr.append(json_data)

d_res = ''
for column_name, column_value in df.items():
    if column_name == '图片名称' and row[column_name] == '诊断结果':
        d_res = row['方向左侧']

filter_arr = [data for data in jarr if data["imageName"] != '诊断结果']
for item in filter_arr:
    item['feature']['诊断结果'] = d_res
    print(item)

data = {'imageName': 'caorunkai_202306131049166292227841.jpg',
        'feature': {'方向': [{'左侧': 0}, {'右侧': 1}],
                    '鼻腔粘膜': [{'未见异常': 0}, {'淡红': 0},
                                 {'充血': 0}, {'苍白': 0},
                                 {'水肿': 0}, {'萎缩': 0}],
                    '鼻中隔': [{'未见异常': 0}, {'居中': 0},
                               {'偏曲左侧': 0}, {'偏曲右侧': 0},
                               {'偏曲S型': 0}, {'嵴突左侧': 0},
                               {'嵴突右侧': 0}, {'棘突左侧': 0},
                               {'棘突右侧': 0},
                               {'黎氏区充血左侧': 0},
                               {'黎氏区充血右侧': 0},
                               {'穿孔左侧': 0}, {'穿孔右侧': 0},
                               {'左侧黎氏区充血': 1}],
                    '总鼻道/鼻腔': [{'未见异常': 0},
                                    {'分泌物见水样': 0},
                                    {'分泌物见脓性': 0},
                                    {'分泌物见粘性': 0},
                                    {'见肿物暗红': 0},
                                    {'见肿物充血': 0},
                                    {'见肿物灰白': 0},
                                    {'见肿物光滑': 0},
                                    {'见肿物不平': 0},
                                    {'见肿物粗糙': 0}],
                    '下鼻甲': [{'未见异常': 0}, {'无肿大': 0},
                               {'肥大': 0}, {'桑甚样改变': 0},
                               {'息肉样变': 0}, {'缺失': 0},
                               {'充血': 0}, {'肿胀': 0}],
                    '中鼻甲': [{'未见异常': 0}, {'无肿大': 0},
                               {'肥大': 0}, {'泡状': 0},
                               {'息肉样变': 0}, {'缺失': 0},
                               {'充血': 0}, {'肿胀': 0}],
                    '中鼻道': [{'未见异常': 1}],
                    '嗅裂': [{'未见异常': 0}, {'充血': 0},
                             {'肿胀': 0}, {'分泌物见水样': 0},
                             {'分泌物见脓性': 0},
                             {'分泌物见粘性': 0},
                             {'见肿物暗红': 0},
                             {'见肿物充血': 0},
                             {'见肿物灰白': 0},
                             {'见肿物光滑': 0},
                             {'见肿物不平': 0},
                             {'见肿物粗糙': 0}],
                    '鼻咽': [{'未见异常': 0}, {'充血': 0},
                             {'肿胀': 0}, {'分泌物见水样': 0},
                             {'分泌物见脓性': 0},
                             {'分泌物见粘性': 0},
                             {'见肿物暗红': 0},
                             {'见肿物充血': 0},
                             {'见肿物灰白': 0},
                             {'见肿物光滑': 1},
                             {'见肿物不平': 0},
                             {'见肿物粗糙': 0},
                             {'见水样分泌物': 1}],
                    '诊断结果': 123}}
